<?php
namespace plugin\entity_manager\src;

/**
 * The MIT License
 *
 * Copyright (c) <year> <copyright holders>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author Damiano Ciarla
 * @package plugin
 * @subpackage entity_manager/class
 */
class EntityValidator
{
	/**
	 * Istanza della entity da validare
	 *
	 * @var AbstractEntity
	 */
    private $_entity;

	/**
	 * Lista degli errori trovati
	 *
	 * @var array
	 */
	private $_errors = array();

	/**
	 * Inizializza una nuova istanza del validatore prendendo in ingresso la entity
	 * su cui effettuare i controlli
	 *
	 * @param AbstractEntity $entity Istanza della entity da validare
	 */
	public function __construct(AbstractEntity $entity)
	{
		$this->_entity = $entity;
	}

	/**
	 * Metodo che si occupa di validare la entity servendosi dei metodi presenti
	 * nella libreria "\bxd\lib\validator\Validator" estesa da questa classe. Il risultato è
	 * un valore booleano che indica se i controlli sono passti o meno
	 *
	 * @return boolean
	 */
	public function validate()
	{
		$properties = $this->_entity->getProperty();

		$validator = new \bxd\lib\validator\ValidatorManager();

		$valueToCheck = array();

		foreach($properties["FIELDS"] as $name => $property)
		{
			$value		= $property["value"];
			$properties = $property["properties"];

			$valueToCheck[$name] = $value;

			$field = $validator->addField($name);

			if($value !== null)
			{
				switch($properties["TYPE"])
				{
					case "int":
					case "smallint":
					case "mediumint":
					case "bigint":
						$field->addRule(\bxd\lib\validator\Validator::CHECK_INTEGER);
						break;

					case "decimal":
					case "float":
					case "double":
					case "real":
						$field->addRule(\bxd\lib\validator\Validator::CHECK_FLOAT);
						break;

					case "datetime":
					case "timestamp":
						$field->addRule(\bxd\lib\validator\Validator::CHECK_DATETIME);
						break;

					case "date":
						$field->addRule(\bxd\lib\validator\Validator::CHECK_DATE, "yyyy-mm-dd");
						break;

					case "enum":
						$field->addRule(\bxd\lib\validator\Validator::CHECK_IN_VALUE, $properties["LENGTH"]);
						break;
				}

				if(isset($properties["LENGTH"]) && $properties["TYPE"] != "enum")
				{
					$field->addRule(\bxd\lib\validator\Validator::CHECK_MAX_LENGTH, $properties["LENGTH"]);
				}
			}
			elseif(!isset($properties["PRIMARY"]) && !isset($properties["AUTO_INCREMENT"]) && !isset($properties["DEFAULT"]) && isset($properties["NOT_NULL"]))
			{
				$field->addRule(\bxd\lib\validator\Validator::CHECK_REQUIRED);
			}
		}

		$result = $validator->validate($valueToCheck);

		$this->_errors = $validator->getErrors();

		return $result;
	}

	/**
	 * Metodo che restituisce la lista degli errori trovati nella validazione
	 *
	 * @return array
	 */
	public function getErrors()
	{
		return $this->_errors;
	}
}
?>
